//package bean.log;
//
//import bean.response.OperateTypeEnum;
//import com.epoch.dao.OperateLogDao;
//import utils.IpUtil;
//import io.swagger.annotations.ApiOperation;
//import org.aspectj.lang.JoinPoint;
//import org.aspectj.lang.annotation.AfterReturning;
//import org.aspectj.lang.annotation.AfterThrowing;
//import org.aspectj.lang.annotation.Aspect;
//import org.aspectj.lang.annotation.Pointcut;
//import org.aspectj.lang.reflect.MethodSignature;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.scheduling.annotation.Async;
//import org.springframework.stereotype.Component;
//import org.springframework.web.context.request.RequestAttributes;
//import org.springframework.web.context.request.RequestContextHolder;
//
//import javax.servlet.http.HttpServletRequest;
//import java.lang.reflect.Method;
//import java.sql.Date;
//
///**
// * Created with IntelliJ IDEA.
// *
// * @Author: 蒋丹
// * @Date: 2021/11/17/10:18
// * @Description:
// */
//@Aspect
//@Component
//public class OperateLogAspect {
//
//    @Autowired
//    private OperateLogDao operateLogDao;
//
//    private static  final Logger log = LoggerFactory.getLogger(OperateLogAspect.class);
//
//    @Pointcut("execution(* com.epoch.controller..*.*(..))")
//    public void logPointCut(){}
//
//    @AfterReturning(pointcut = "logPointCut()")
//    public void doAfterReturn(JoinPoint joinPoint){
//        handleLog(joinPoint,null);
//    }
//
//    @AfterThrowing(value="logPointCut()",throwing = "e")
//    public void doAfterThrow(JoinPoint joinPoint,Exception e){
//        handleLog(joinPoint,e);
//    }
//
//    private  void handleLog(JoinPoint joinPoint,Exception e){
//        RequestAttributes requestAttributes= RequestContextHolder.getRequestAttributes();
//        assert requestAttributes != null;
//        HttpServletRequest request=(HttpServletRequest) requestAttributes.resolveReference(RequestAttributes.REFERENCE_REQUEST);
//        assert request != null;
//        StringBuffer requestUrl =request.getRequestURL();
//        String requestMethod=request.getMethod();
//        String ipAddress= IpUtil.getIp(request);
//        String className=joinPoint.getTarget().getClass().getSimpleName();
//        MethodSignature signature=(MethodSignature) joinPoint.getSignature();
//        Method method =signature.getMethod();
//        String methodName=method.getName();
//        Class<?>[] methodParameterType=method.getParameterTypes();
//        StringBuilder parameters=new StringBuilder();
//        for(Class<?> parameterType:methodParameterType){
//            if(parameters.length()==0){
//                parameters.append(parameterType.getSimpleName()).append(".class");
//            }else{
//                parameters.append(",").append(parameterType.getSimpleName()).append(".class");
//            }
//        }
//        String parameterTypes=parameters.toString();
//
//        //设置操作板块
//        ApiOperation annotation=method.getAnnotation(ApiOperation.class);
//        String describe=null;
//        if(annotation==null){
//            describe= "其他板块";
//        }else{
//            describe=annotation.value();
//        }
//        //设置操作类型
//        String operateType="";
//        String delete = "d";
//        String update = "u";
//        String add = "a";
//        if(methodName.startsWith(delete)){
//            operateType = OperateTypeEnum.DELETE_TYPE.getType();
//        }else if(methodName.startsWith(update)){
//            operateType = OperateTypeEnum.UPDATE_TYPE.getType();
//        }else if (methodName.startsWith(add)){
//            operateType = OperateTypeEnum.ADD_TYPE.getType();
//        }else{
//            operateType = OperateTypeEnum.SELECT_TYPE.getType();
//        }
//
//        insertOperationLog(className,methodName,parameterTypes,requestUrl.toString(),ipAddress,describe,operateType);
//    }
//
//    @Async
//    public void insertOperationLog(String className, String methodName, String parameterTypes, String requestUrl, String ip, String describe, String operateType) {
//        Date operationTime = new Date(System.currentTimeMillis());
//        // 生成一个操作信息类
//        OperateLog operateLog = new OperateLog();
//        //类名、方法及参数
//        operateLog.setClassName(className);
//        operateLog.setMethodName(methodName);
//        operateLog.setParameterTypes(parameterTypes);
//        //请求url
//        operateLog.setRequestUrl(requestUrl);
//        //请求ip
//        operateLog.setIp(ip);
//        //操作描述 -- 板块
//        operateLog.setDescribe(describe);
//        //请求时间
//        operateLog.setOperationTime(operationTime);
//        //请求类型 增删改查
//        operateLog.setOperateType(operateType);
//
//        // 将操作信息插入数据库
//        operateLogDao.insertOperateLog(operateLog);
////        operateLogDao.insertOperateLog(className,methodName,parameterTypes,requestUrl,ip,describe,operationTime,operateType);
//    }
//}
